<!DOCTYPE html>
<uieip type="page" value="login" debug="true" timeout="5000"></uieip>
<uieip type="reg" value="console"></uieip>

<html>

<head>
    <uieip type="file" value="inc" src="/yqwin/easyui/include.txt" cache="true" minute="1440"></uieip>
    <uieip type="file" value="css" src="/yqwin/css/yqwin.min" cache="true"></uieip>
    <uieip type="file" value="js" src="/yqwin/src/yqwin.min" cache="false"></uieip>
    <script src="/yqwin/src/yq.flowedit.js"></script>
    <script src="/yqwin/src/yq.flowview.js"></script>
    <title>工作流管理</title>
    <style>
        #cc_wf{
            background-color: var(--body-bg-color);
        }
    </style>
</head>

<body>
    <div id="cc" class="easyui-layout" style="width:100%;height:600px;">
        <div id="cc_wf" data-options="region:'west',title:'流程',split:true" style="width:255px;overflow: hidden;">
            <ul id="tt_main">
            </ul>
            <div style="padding-top: 5px;">
                <table id="tb_ver" style="width:100%;height: 500px;"></table>
            </div>
        </div>
        <div data-options="region:'center'" style="padding:0px;">
            <div id="div_flowedit"></div>
        </div>
    </div>
    <script>
        var dbname = "yqbase";

        var page_fun = {
            setResizeTime: undefined,
            init: function () {
                page_fun.resize();
                $(window).resize(function () {
                    page_fun.resize();
                });
                var $west = $('#cc').layout("panel", "west");
                $west.panel("options").onResize = function (width, height) {
                    if (page_fun.setResizeTime === undefined) {
                        page_fun.setResizeTime = setTimeout(function () {
                            console.log("sssss");
                            page_fun.resize();
                        }, 100);
                    }
                }

                $('#tt_main').yqTree("loadData");
            },
            resize: function () {
                $('#cc').layout('resize', {
                    width: window.innerWidth,
                    height: window.innerHeight
                });

                var $west = $('#cc').layout("panel", "west");
                var l_width = $west.width();
                var l_height = $west.height() - 5;
                main_fun.resize({
                    width: l_width,
                    height: l_height - 250
                });

                ver_fun.resize({
                    width: l_width,
                    height: 250
                });

                var $center = $('#cc').layout("panel", "center");
                var center_w = $center.width();
                var center_h = $center.height();

                flowedit_fun.resize({
                    width: center_w,
                    height: center_h
                });

                if (!(page_fun.setResizeTime === undefined)) {
                    clearInterval(page_fun.setResizeTime);
                    page_fun.setResizeTime = undefined;
                };
            }
        }

        $(function () {
            $.yqDbList({ ajaxUrl: "_db.html" },
                function (data) {
                    if (data.error == 0) {
                        dbname = data.rows[0].name;
                        console.log(data, dbname);
                        //init_main();
                        main_fun.init();
                    } else {
                        alert(data.info);
                    }
                });
        });

        var main_fun = {
            id: "tt_main",
            init: function () {
                var opt = {
                    ajaxUrl: "/_db.html",
                    dbname: dbname,
                    tbname: "sys_wfmain",
                    keyId: "sid",
                    bindingKeyId: null,
                    bindingValue: null,
                    pKeyId: "pid",
                    pKeyValue: null,

                    loadDataType: 2,
                    displayMember: "name",
                    loadData: false,

                    menuType: 3,

                    overflowY: true,

                    editMode: 2,
                    editModeW: 600,
                    editModeH: 450,
                    editModeNo: -1,

                    beforeBindingTitle: function (titles, callback) {
                        //console.log("beforeBindingTitle", titles);
                        callback(titles);
                    },
                    afterBindingTitle: function () {
                        page_fun.init();
                    },

                    beforeSetdata: function (data, callback) {
                        //console.log("beforeSetdata", data);
                        callback(data)
                    },
                    afterSetdata: function (data) {
                        //ver_fun.load();
                    },
                    formatter: function (node) {
                        //menu_type 0 文件夹,1 菜单界面,2 弹出界面
                        var wf_type = node.attributes["wf_type"];
                        if (wf_type === undefined || wf_type == null) {
                            wf_type = 2;
                        }
                        var text = node.text;
                        if (node.text == null) {
                            text = node.attributes["describe"];
                        }

                        return text;
                    },
                    onClick: function (node) {     //节点点击
                        ver_fun.load();
                    },
                };

                $('#' + main_fun.id).yqTree(opt);
            },
            get_row: function () {
                var row = $('#' + main_fun.id).yqTree("getSelected");
                if (row == null) {
                    return null;
                }
                return row.attributes;
            },
            get_val: function () {
                var row = main_fun.get_row();
                if (row == null) {
                    return null;
                }
                return row["sid"];
            },
            resize: function (res) {
                $('#tt_main').yqTree("resize", res);
            }
        }

        var ver_fun = {
            id: "tb_ver",
            init: false,
            runing: false,
            bindingValue: "*",
            load: function () {
                if (ver_fun.runing) {
                    return;
                }
                ver_fun.runing = true;
                if (ver_fun.init) {
                    ver_fun.load_data();
                    return;
                }

                var opt = {
                    ajaxUrl: "/_db.html",
                    dbname: dbname,
                    tbname: "sys_wfver",
                    keyId: "sid",
                    bindingKeyId: "mainid",
                    bindingValue: null,

                    addWhere: "",
                    order: "queue",

                    editMode: 3,
                    editModeW: 600,
                    editModeH: 450,
                    editModeNo: 1,
                    loadData: false,
                    isPage: false,

                    isEdit: true,
                    copy: true,
                    add: true,
                    upp: false,
                    del: true,
                    isdel: false,
                    isdelEdit: false,
                    move: true,

                    editLoadData: true,

                    isSimple: false,
                    isSeek: false,

                    //linkField: ["name"],

                    afterBindingTitle: function () {
                        ver_fun.init = true;
                        ver_fun.load_data();
                    },
                    onBeforeShowEdit: function (titles, action, row, callback) {
                        callback(true, titles, row);
                    },
                    afterSetdata: function (data) {
                        ver_fun.runing = false;
                    },
                    afterDataAction: function (rows, res) {    //修改之后              
                    },
                    onClickRow: function (index, row) {
                        flowedit_fun.load();
                    }
                };
                $("#" + ver_fun.id).yqDataGrid(opt);
            },
            load_data: function () {
                ver_fun.resize();

                flowedit_fun.is_edit(false);
                flowedit_fun.clear();

                var bindingValue = main_fun.get_val();
                if ((bindingValue === null ? "" : bindingValue) == (ver_fun.bindingValue === null ? "" : ver_fun.bindingValue)) {
                    ver_fun.runing = false;
                    return;
                }

                ver_fun.bindingValue = bindingValue;

                if (bindingValue == null) {
                    $("#" + ver_fun.id).yqDataGrid("setBindingValue", null);
                    $("#" + ver_fun.id).yqDataGrid("clear");
                } else {
                    var row = main_fun.get_row();
                    var wf_type = row["wf_type"];
                    if (wf_type === undefined || wf_type == null) {
                        wf_type = 2;
                    }
                    if (wf_type == 1) {
                        $("#" + ver_fun.id).yqDataGrid("setBindingValue", bindingValue);
                        $("#" + ver_fun.id).yqDataGrid("seek", "");
                    } else {
                        $("#" + ver_fun.id).yqDataGrid("setBindingValue", null);
                        $("#" + ver_fun.id).yqDataGrid("clear");
                    }
                }
            },
            get_row: function () {
                var row = $('#' + ver_fun.id).yqDataGrid("getSelected");
                return row;
            },
            get_val: function () {
                var row = ver_fun.get_row();
                if (row == null) {
                    return null;
                }
                return row["sid"];
            },
            resize_res: null,
            resize: function (res) {
                if (!(res === undefined)) {
                    ver_fun.resize_res = res;
                }

                if (ver_fun.init) {
                    $('#' + ver_fun.id).yqDataGrid("resize", ver_fun.resize_res);
                }
            }
        }

        var flowedit_fun = {
            id: "div_flowedit",
            init: false,
            runing: false,
            bindingValue: null,
            info_fields: ["ver", "current", "name", "describe", "func", "db_name",
                "tb_name", "state_field", "state_func", "state_start", "state_start_name",
                "parameter", "case_when", "remark"],
            load: function () {
                if (flowedit_fun.runing) {
                    return;
                }
                flowedit_fun.runing = true;
                if (flowedit_fun.init) {
                    flowedit_fun.load_data();
                    return;
                }

                var opt = {
                    menus: [{
                        id: "save",
                        text: "下载",
                        iconCls: "yqwin-btn-icon fa fa-download",
                        onClick: function () {
                            $('#div_flowedit').yqFlowEdit("save", "flow.json");
                        }
                    }, {
                        id: "open",
                        text: "打开",
                        iconCls: "yqwin-btn-icon fa fa-folder-open",
                        onClick: function () {
                            $('#div_flowedit').yqFlowEdit("load");
                        }
                    }, {}, {
                        id: "savedata",
                        text: "保存[数据库]",
                        iconCls: "yqwin-btn-icon fa fa-floppy-o",
                        onClick: function () {
                            var json_ = $('#div_flowedit').yqFlowEdit("getValue");

                            var ver_row = $("#tb_ver").yqDataGrid("getSelected");
                            ver_row = $.extend(ver_row, json_.info);

                            if (typeof ver_row["parameter"] == "object") {
                                ver_row["parameter"] = JSON.stringify(ver_row["parameter"]);
                            } else {
                                delete ver_row["parameter"];
                            }

                            delete json_.info.parameter
                            ver_row["json"] = JSON.stringify(json_);

                            $("#tb_ver").yqDataGrid("uppRow", ver_row);
                            //console.log(ver_row);
                        }
                    }, {
                        id: "test",
                        text: "测试",
                        iconCls: "yqwin-btn-icon fa fa-paper-plane",
                        onClick: function () {
                            flowedit_fun.test_wf();
                        }
                    }],
                    value: {},
                    readonlyEnable: false,
                    btnMini: true,
                    afterBindingTitle: function () {
                        flowedit_fun.init = true;
                        flowedit_fun.is_edit(false);
                        flowedit_fun.load_data();
                    },
                };

                $('#' + flowedit_fun.id).yqFlowEdit(opt);
            },
            load_data: function () {
                flowedit_fun.resize();

                var bindingValue = ver_fun.get_val();
                if ((bindingValue === null ? "" : bindingValue) == (flowedit_fun.bindingValue === null ? "" : flowedit_fun.bindingValue)) {
                    flowedit_fun.runing = false;
                    return;
                }

                flowedit_fun.bindingValue = bindingValue;

                if (bindingValue === null) {
                    flowedit_fun.is_edit(false);

                    $('#' + flowedit_fun.id).yqFlowEdit("setValue", "{}");
                } else {
                    flowedit_fun.is_edit(true);

                    var row = ver_fun.get_row();

                    var _json = {};

                    if (!(row["json"] === undefined)) {
                        _json = (row["json"] === null ? {} : JSON.parse(row["json"]));
                        if (_json.info === null || _json.info === undefined) {
                            _json.info = {};
                        }
                        if (_json.node === null || _json.node === undefined) {
                            _json.node = [];
                        }
                        if (_json.link === null || _json.link === undefined) {
                            _json.link = [];
                        }

                        for (var index = 0; index < flowedit_fun.info_fields.length; index++) {
                            var _sval = flowedit_fun.info_fields[index];
                            if (!(row[_sval] === undefined || row[_sval] === null)) {
                                if (_sval == "parameter") {
                                    _json.info[_sval] = (row[_sval] === null ? null : JSON.parse(row[_sval]));
                                } else {
                                    _json.info[_sval] = row[_sval];
                                }
                            }
                        }

                        $('#' + flowedit_fun.id).yqFlowEdit("setValue", _json);
                    }
                }

                flowedit_fun.runing = false;
            },
            clear: function () {
                flowedit_fun.set_data(null);
            },
            set_data: function (data) {
                if (!flowedit_fun.init) {
                    return;
                }

                if (data === null) {
                    $('#' + flowedit_fun.id).yqFlowEdit("setValue", "{}");
                } else {
                    $('#' + flowedit_fun.id).yqFlowEdit("setValue", data);
                }
            },
            is_edit: function (tf) {
                if (!flowedit_fun.init) {
                    return;
                }

                var opts = $('#' + flowedit_fun.id).yqFlowEdit("options");
                for (var index = 0; index < opts.menus.length; index++) {
                    var menu = opts.menus[index];
                    $('#' + flowedit_fun.id).yqFlowEdit((tf ? "menuEnable" : "menuDisable"), menu.id);
                }

                $('#' + flowedit_fun.id).yqFlowEdit("isEdit", tf);
            },
            resize_res: null,
            resize: function (res) {
                if (!(res === undefined)) {
                    flowedit_fun.resize_res = res;
                }
                if (flowedit_fun.init) {
                    $('#' + flowedit_fun.id).yqFlowEdit("resize", flowedit_fun.resize_res);
                }
            },
            test_wf: function () {
                //获取流程实例
                var ver_row = ver_fun.get_row();
                var init_data = {
                    verId: ver_row["sid"],   //流程版本id
                    case_when: ver_row["case_when"] === undefined ? null : ver_row["case_when"],     //流程过滤条件
                    params: {},
                    Parameter: ((ver_row["parameter"] === undefined || ver_row["parameter"] === null) ? {} : JSON.parse(ver_row["parameter"])),
                    tbId: "sid1111-1-1" //表记录id
                }

                console.log("init_data", init_data, ver_row);

                $_yqFlow.wfInit(init_data, function (res) {
                    console.log("wfInit", res);
                    if (res.error == 0) {
                        show_test(res.wf);
                    } else {
                        $.yqMessager("error", res.info);
                    }
                }, true);

                function show_test(data) {
                    var $pp = $("#" + flowedit_fun.id);
                    var prevent_e = $.getPreventDefault($pp);
                    prevent_e.clientY -= ($pp.height() - 50);
                    prevent_e.clientX += 50;
                    var h_ = $pp.height() - 100;
                    var w_ = $pp.width() - 100;
                    if (h_ < 400) {
                        h_ = 400;
                    }
                    if (w_ < 300) {
                        w_ = 300;
                    }
                    var _pops = {
                        prevent_e: prevent_e,
                        mdiinfo: $.yqGetWindParams(),
                        title: "流程测试",
                        width: w_,
                        height: h_,
                        closeBtn: false,
                        closeBtnText: $.fn.yqUiExtText.btnNo,
                        onOpen: function ($body) {
                            var $div_wf = $('<div class="div_wf" style="width:100%;height:100%;"></div>');
                            $body.append($div_wf);
                            if (window.top != window.self) {
                                $div_wf = window.top.$("#" + $body.attr("id")).find(".div_wf");
                            }

                            console.log(data);

                            var opt = {
                                value: data,
                                type: 1,  //0-本地测试,1-服务端测试,2-执行流程   
                                beforeBindingTitle: function (callback) {
                                    callback();
                                }, //标题绑定前
                                afterBindingTitle: function () { },   //标题绑定后
                            };

                            $div_wf.yqFlowView(opt);
                        }
                    };
                    if (window.top == window.self) {
                        $.yqDialog(_pops);
                    }
                    else {
                        window.top.$.yqDialog(_pops);
                    }
                }
            }
        };

    </script>
</body>

</html>